﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace simulate_control.Basic
{
    #region USING
    using simulate_control.Repositories;
    using simulate_control.Models;
    using simulate_control.Helpers;
    using DevComponents.DotNetBar;
    #endregion

    public partial class FrmPlotterSetup : Form
    {
        #region VARIABLES
        public EventHandler Handler { get; set; }
        private Dictionary<int, string> dict = new Dictionary<int, string>();

        // Bien danh dau hien thoi dang lua chon item nao
        private int Selected = 0;

        // Select TrackRec
        private FrmSelectOptions frmSelectOptions;

        // Bien danh dau item da di vao trang thai edit hay chua
        private bool EnterEdit = false;

        // Ki tu trong item duoc lua chon 
        private int SelectedCharacterPos = 0;

        // 3 mang dung de phan loai item thuoc ve nhom nao: time, character hay value
        private string[] arrTimeVal = new string[] { };
        private string[] arrChar = new string[] { "lblBrgRef", "lblMagVar", "lblWyptName" };
        private string[] arrVal = new string[] { "lblInterval", "lblResetTrip", "lblTrackMemoryUsed" };

        // Bien chua dung danh sach ten cac label item
        private string[] lstHighlightLabelName = new string[] { "lblTractRec", "lblInterval", "lblBrgRef", "lblMagVar", "lblWyptName", "lblResetTrip", "lblTrackMemoryUsed" };
        #endregion

        #region METHODS
        public FrmPlotterSetup()
        {
            InitializeComponent();
        }

        public void ProcessKeyEvent(KEY keyEvent)
        {
            // Da vao trang thai edit
            if (EnterEdit)
            {
                if (Selected == 0)
                {
                    // Uy quyen cho form duoc goi xu ly key event
                    frmSelectOptions.Focus();
                    frmSelectOptions.ProcessKeyEvent(keyEvent);
                    // Viec thoat khoi trang thai edit duoc thuc hien khi goi ham UpdateOptionValue
                }
                else
                {
                    if (keyEvent == KEY.ENTER)
                    {
                        Utils.RetrieveToNextItemPos(pnlMain, dict, ref Selected);
                        // Thoat khoi trang thai edit
                        EnterEdit = false;
                    }
                    else
                    {
                        // Cap nhat lai gia tri cho item duoc lua chon
                        var lbl = Utils.GetSelectedHighlightLabel(pnlMain, dict[Selected]);
                        Utils.ChangeCharacterPos(ref lbl, dict, Selected, ref SelectedCharacterPos, keyEvent, arrTimeVal, arrChar, arrVal);
                    }
                }
            }
            else// Chua vao trang thai edit
            {
                if (keyEvent == KEY.ENTER)
                {
                    if (Selected == 0)
                    {
                        ShowSelectedOptionFrm();
                    }
                    else
                    {
                        SelectedCharacterPos = 0;
                        var lbl = Utils.GetSelectedHighlightLabel(pnlMain, dict[Selected]);
                        Utils.UpdateHighlightLabels(ref lbl, SelectedCharacterPos);
                    }
                    // Di vao trang thai edit
                    EnterEdit = true;
                }
                else
                {
                    // Lua chon item de edit
                    Utils.ChangeSelectItemPos(pnlMain, dict, ref Selected, keyEvent);
                }
            }
        }

        private void UpdateOptionValue(object sender, EventArgs e)
        {
            var eventType = (TransferDataEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });
            var lblTractRec = Utils.GetSelectedHighlightLabel(pnlMain, "lblTractRec");
            lblTractRec.LabelText = comp[0];
            lblTractRec.LabelMaxLength = comp[0].Length;
            lblTractRec.Refresh();
            // Thoat khoi trang thai edit
            EnterEdit = false;
        }

        public void ShowSelectedOptionFrm()
        {
            var lstData = new List<string>();
            lstData.Add("OFF");
            lstData.Add("DISTANCE");
            lstData.Add("AUTO");

            frmSelectOptions = new FrmSelectOptions(lstData);
            frmSelectOptions.Handler += UpdateOptionValue;
            frmSelectOptions.Show();
        }

        private void FrmPlotterSetup_Load(object sender, EventArgs e)
        {
            Utils.InitHighlightLabels(ref pnlMain, lstHighlightLabelName);
            Utils.InitDict(ref dict, lstHighlightLabelName);
            Utils.ChangeSelectItem(pnlMain, dict, 0);
            EnterEdit = false;
        }
        #endregion
    }
}
